<?php
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph.php';

/*
// TTF Font families
FF_COURIER
FF_VERDANA
FF_TIMES
FF_COMIC
FF_ARIAL
FF_GEORGIA
FF_TREBUCHE

// Gnome Vera font
// Available from http://www.gnome.org/fonts/
FF_VERA
FF_VERAMONO
FF_VERASERIF

// Chinese font
FF_SIMSUN
FF_CHINESE
FF_BIG5

// Japanese font
FF_MINCHO
FF_PMINCHO
FF_GOTHIC
FF_PGOTHIC

// Hebrew fonts
FF_DAVID
FF_MIRIAM
FF_AHRON

// Dejavu-fonts http://sourceforge.net/projects/dejavu
FF_DV_SANSSERIF
FF_DV_SERIF
FF_DV_SANSSERIFMONO
FF_DV_SERIFCOND
FF_DV_SANSSERIFCOND

// FF_SPEEDO
FF_DIGITAL
FF_COMPUTER
FF_CALCULATOR

// TTF Font styles
FS_NORMAL
FS_BOLD
FS_ITALIC
FS_BOLDIT
FS_BOLDITALIC
*/

/**
 * JPGraph Wrapper
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
abstract class Nina_Graph_Jpgraph extends Nina_Graph implements Nina_Graph_Adapter
{
	/**
	 * JPGraph plot object
	 * @access protected
	 * @var Plot
	 */
	protected $plot;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->graph->img->SetAntiAliasing();
		$this->graph->img->SetColor('blue');
	}

	/**
	 * Add an external plot object
	 * @param Graph $graph The graph from which to extract the new plot
	 */
	public function addPlot($graph) {
		$this->graph->add($graph->getPlot());
	}

	/**
	 * Get the plot object
	 * @return Plot
	 */
	public function getPlot() {
		return $this->plot;
	}

	/**
	 * Hide the Legend
	 */
	public function hideLegend() {
		$this->plot->HideLegend();
	}

	/**
	 * Set the legend text
	 * @param string $legend The legend text
	 */
	public function setLegend($legend) {
		$this->plot->SetLegend($legend);
	}

	/**
	 * Set the legend border
	 */
	public function setLegendBorder() {
		$this->graph->legend->SetFrameWeight(1);
	}

	/**
	 * Set the legend font
	 * @param string $face The font face
	 * @param string $weight The font weight
	 * @param integer $size The font size
	 */
	public function setLegendFont($face, $weight, $size) {
		$this->graph->legend->SetFont($face, $weight, $size);
	}

	/**
	 * Set the theme
	 * @param string $theme The theme
	 */
	public function setTheme($theme) {
		$this->plot->SetTheme($theme);
	}

	/**
	 * Set the shadow
	 * @param string [$color='darkgray'] The color
	 * @param integer [$width=4] The width
	 */
	public function setShadow($color = 'darkgray', $width = 4) {
		$this->graph->SetShadow(true, $width, $color);
	}

	/**
	 * Set the margin
	 * @param integer [$left=10] The left margin
	 * @param integer [$right=null] The right margin
	 * @param integer [$top=null] The top margin
	 * @param integer [$bottom=null] The bottom margin
	 */
	public function setMargin($left = 10, $right = null, $top = null, $bottom = null) {

		if(!$right)
			$right = $left;
		if(!$top)
			$top = $left;
		if(!$bottom)
			$bottom = $top;

		$this->graph->img->SetMargin($left, $right, $top, $bottom);
	}

	/**
	 * Set the title margin
	 * @param integer [$margin=10] The margin width
	 */
	public function setTitleMargin($margin = 10) {
		$this->graph->title->SetMargin($margin);
	}

	/**
	 * Set the title font
	 * @param string $face The font face
	 * @param string $weight The font weight
	 * @param integer $size The font size
	 */
	public function setTitleFont($face, $weight, $size) {
		$this->graph->title->SetFont($face, $weight, $size);
	}

	/**
	 * Set the title color
	 * @param string $color The color
	 */
	public function setTitleColour($colour) {
		$this->graph->title->SetColor($colour);
	}

	/**
	 * Set the title alignment
	 * @param string [$align='center'] The alignment
	 */
	public function setTitleAlign($align = 'center') {
		$this->graph->title->SetAlign('center');
	}

	/**
	 * Set the title paragraph alignment
	 * @param string [$align='center'] The alignment
	 */
	public function setTitleParagraphAlign($align = 'center') {
		$this->graph->title->SetParagraphAlign('center');
	}

	/**
	 * Set the title angle
	 * @param integer [$angle=0] The angle
	 */
	public function setTitleAngle($angle = 0) {
		$this->graph->title->SetAngle($angle);
	}

	/**
	 * Set the subtitle
	 * @param string $title The subtitle
	 */
	public function setSubtitle($title) {
		$this->graph->subtitle->Set($title);
	}

	/**
	 * Set the subtitle margin
	 * @param string [$margin=10] The margin
	 */
	public function setSubtitleMargin($margin = 10) {
		$this->graph->subtitle->SetMargin($margin);
	}

	/**
	 * Set the subtitle font
	 * @param string $face The font face
	 * @param string $weight The font weight
	 * @param integer $size The font size
	 */
	public function setSubtitleFont($face, $weight, $size) {
		$this->graph->subtitle->SetFont($face, $weight, $size);
	}

	/**
	 * Set the subtitle color
	 * @param string $color The color
	 */
	public function setSubtitleColour($colour) {
		$this->graph->subtitle->SetColor($colour);
	}

	/**
	 * Set the subtitle alignment
	 * @param string [$align='center'] The alignment
	 */
	public function setSubtitleAlign($align = 'center') {
		$this->graph->subtitle->SetAlign('center');
	}

	/**
	 * Set the subtitle paragraph alignment
	 * @param string [$align='center'] The alignment
	 */
	public function setSubtitleParagraphAlign($align = 'center') {
		$this->graph->subtitle->SetParagraphAlign('center');
	}

	/**
	 * Set the subtitle angle
	 * @param integer [$angle=0] The angle
	 */
	public function setSubtitleAngle($angle = 0) {
		$this->graph->subtitle->SetAngle($angle);
	}

	/**
	 * Set the axis font
	 * @param string $face The font face
	 * @param string $weight The font weight
	 * @param integer $size The font size
	 */
	public function setAxisFont($face, $weight, $size) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetFont($face, $weight, $size);
			$this->graph->yaxis->SetFont($face, $weight, $size);
		}
	}

	/**
	 * Set the axis weight
	 * @param integer $weight The weight
	 */
	public function setAxisWeight($weight) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetWeight($weight);
			$this->graph->yaxis->SetWeight($weight);
		}
	}

	/**
	 * Set the axis color
	 * @param string $color The color
	 * @param string [$label_color=false] The label color
	 */
	public function setAxisColor($color, $label_color = false) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetColor($color, $label_color);
			$this->graph->yaxis->SetColor($color, $label_color);
		}
	}

	/**
	 * Set the x axis title
	 * @param string $title The title
	 */
	public function setXAxisTitle($title) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis))
			$this->graph->xaxis->SetTitle($title);
	}

	/**
	 * Set the y axis title
	 * @param string $title The title
	 */
	public function setYAxisTitle($title) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->yaxis))
			$this->graph->yaxis->SetTitle($title);
	}

	/**
	 * Set the axis title margin
	 * @param string [$margin=10] The margin
	 */
	public function setAxisTitleMargin($margin_x = 10, $margin_y = null) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			if(!$margin_y)
				$margin_y = $margin_x;

			$this->graph->xaxis->SetTitleMargin($margin_x);
			$this->graph->yaxis->SetTitleMargin($margin_y);
		}
	}

	/**
	 * Set the axis title font
	 * @param string $face The font face
	 * @param string $weight The font weight
	 * @param integer $size The font size
	 */
	public function setAxisTitleFont($face, $weight, $size) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->title->SetFont($face, $weight, $size);
			$this->graph->yaxis->title->SetFont($face, $weight, $size);
		}
	}

	/**
	 * Hide the zero y axis label
	 */
	public function hideZeroLabel() {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->yaxis))
			$this->graph->yaxis->HideZeroLabel();
	}

	/**
	 * Set the axis tick size
	 * @param string $major_size The major size
	 * @param string [$minor_size=3] The minor size
	 */
	public function setTickSize($major_size, $minor_size = 3) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetTickSize($major_size, $minor_size);
			$this->graph->yaxis->SetTickSize($major_size, $minor_size);
		}
	}

	/**
	 * Specify text labels for the axis ticks. One label for each data point
	 * @param array $labels_x The labels
	 * @param array [$labels_y=null] The labels
	 */
	public function setTickLabels($labels_x, $labels_y = null) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetTickLabels($labels_x);

			//normally the y axis will just have the numbers
			if($labels_y)
				$this->graph->yaxis->SetTickLabels($labels_y);
		}
	}

	/**
	 * Set the axis label margin
	 * @param integer [$margin_x=10] The x margin
	 * @param integer [$margin_y=null] The y margin
	 */
	public function setLabelMargin($margin_x = 10, $margin_y = null) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			if(!$margin_y)
				$margin_y = $margin_x;

			$this->graph->xaxis->SetLabelMargin($margin_x);
			$this->graph->yaxis->SetLabelMargin($margin_y);
		}
	}

	/**
	 * Set the axis tick interval
	 * @param integer [$margin=10] The margin
	 */
	public function setTickInterval($interval = 1, $start = 0) {
		//Won't have axes if it's a pie chart
		if(isset($this->graph->xaxis)) {
			$this->graph->xaxis->SetTextTickInterval($interval, $start);
			$this->graph->yaxis->SetTextTickInterval($interval, $start);
		}
	}

	/**
	 * Set the plot scale
	 * @param string [$axis_type='lin'] The axis type - lin, int, log or linlin, linlog etc
	 * @param string [$y_min=1] The y axis minimum
	 * @param string [$y_max=1] The y axis maximum
	 * @param string [$x_min=1] The x axis minimum
	 * @param string [$x_max=1] The x axis maximum
	 */
	public function setScale($axis_type = 'lin', $y_min = 1, $y_max = 1, $x_min=1, $x_max=1) {
		$this->graph->SetScale($axis_type, $y_min, $y_max, $x_min, $x_max);
	}

	/**
	 * Set grace (headroom past the max value) as a percentage
	 * @param integer [$grace=10] The grace
	 */
	public function setScaleGrace($grace = 10) {
		$this->setXScaleGrace($grace);
		$this->setYScaleGrace($grace);
	}

	/**
	 * Set y-axis grace (headroom past the max value) as a percentage
	 * @param integer [$grace=10] The grace
	 */
	public function setYScaleGrace($grace = 10) {
		if($this->graph->yscale)
			$this->graph->yscale->SetGrace($grace);
	}

	/**
	 * Set x-axis grace (headroom past the max value) as a percentage
	 * @param integer [$grace=10] The grace
	 */
	public function setXScaleGrace($grace = 10) {
		if($this->graph->xscale)
			$this->graph->xscale->SetGrace($grace);
	}

	/**
	 * Rotate 90 degrees
	 */
	public function rotate() {
		$this->graph->Set90AndMargin();
	}

	/**
	 * Set the plot angle
	 * @param integer [$angle=0] The angle
	 */
	public function setAngle($angle = 0) {
		$this->graph->SetAngle($angle);
	}

	/**
	 * Render the graph to the screen
	 */
	public function render() {
		$this->graph->title->Set($this->title);
		$this->graph->legend->SetPos(0.01, 0.01, 'right', 'top');
		$this->graph->legend->SetColumns(1);
		$this->graph->Add($this->plot);
		$this->graph->Stroke();
	}
}
